const express = require("express");
const file = require("../modules/FileHelper")
const db = require("../modules/DBHelper")

const prodRouter = express.Router();


//获取所有的产品的信息
prodRouter.get("/getlist", async (req, res) => {
    var strGoods = await file.read("./data/goods.json");
    res.json(JSON.parse(strGoods));
})

//根据产品的id查询产品的详细信息
prodRouter.get("/getinfo/:id", async (req, res) => {
    let id = req.params.id;
    var strgoodsList = await file.read("./data/goodsInfo.json");
    var goodInfo = JSON.parse(strgoodsList).find(item => item.gid == id);
    res.json({
        msg: "查询成功",
        code: 200,
        data: goodInfo
    })
})



// 前端发送过来的数据
// gid: 1003
// gimg: "./images/s1.webp"
// gname: "装 无抗安全放心 红白鸡蛋随机发货 预计十天天内发货"
// gnum: "1"
// gprice: "31.8"
// uId: 1


prodRouter.post("/addCart", async (req, res) => {
    //1.得到前端发送的数据
    var oProd = req.body;
    //是否第一次购买  跟购买人的id和购买产品的id ,推动是否是第一次购买
    let sql_query = "SELECT * FROM carts WHERE `pstatus`=1 AND uid=? AND pid=?;";
    let params_query = [
        oProd.uId,
        oProd.gid
    ]

    console.log(oProd);

    let result_query = await db.execute(sql_query, params_query);
    let isOneAdd = result_query.length == 0 //如果数组的长度,为0,说明是第一次购买

    console.log(result_query);
    console.log(isOneAdd);
    if (isOneAdd) {   //如果是第一次购买,就添加 insert
        //2.准备sql语句
        let sql_add = "INSERT INTO carts (`pId`,`pname`,`pprice`,`pnum`,`ptotal`,`pimg`,`uId`) ";
        sql_add += " VALUES(?,?,?,?,?,?,?);"
        //3.配置参数
        let params = [
            oProd.gid,
            oProd.gname,
            oProd.gprice,
            oProd.gnum,
            oProd.gnum * oProd.gprice,
            oProd.gimg,
            oProd.uId
        ]
        //4.执行slq语句,判断结果
        var result_insert = await db.execute(sql_add, params);
        let isInsert = result_insert && result_insert.affectedRows >= 1;
        res.json({
            msg: isInsert ? "加入购物车成功!i" : "加入购物车失败i",
            code: 200
        })
    } else {
        //不是第一次,根据购买人,产品id,确定修改 购买的 数量 
        let sql_update = "UPDATE carts SET `pnum`=pnum+? ,`ptotal`=pnum*`pprice` WHERE `pstatus`=1 AND uid=? AND pid=?;"
        let params_update = [
            oProd.gnum,
            oProd.uId,
            oProd.gid
        ];
        let result_update = await db.execute(sql_update, params_update);

        let isUpate = result_update && result_update.affectedRows >= 1

        res.json({
            msg: isUpate ? "加入购物车成功!u" : "加入购物车失败u",
            code: 200
        })
    }
})


//根据登录人的id查询他购买的商品
prodRouter.get("/query", async (req, res) => {
    var oProd = req.query;
    let sql = "SELECT * FROM carts WHERE uid=? AND pstatus=1;";
    let params = [
        oProd.uId
    ]
    let result = await db.execute(sql, params);

    res.json({
        msg: "查询成功",
        code: 200,
        data: result
    })

})

//根据购物车id,进行删除
prodRouter.post("/delete", async (req, res) => {
    var cId = req.body.cId;

    let sql =  "UPDATE `carts` SET `pstatus`=0  WHERE cid=? AND pstatus=1 ; ";
    let params = [cId];

    let result = await db.execute(sql, params);
    let isDelete = result && result.affectedRows >= 1;

    res.json({
        msg: isDelete ? "删除成功" : "删除失败",
        code: 200
    })
})

//根据购物车的id修改 购买的数量

prodRouter.post("/modify", async (req, res) => {

    let sql = "UPDATE `carts` SET `pnum`=? ,`ptotal`=pnum*`pprice` WHERE cid=? AND pstatus=1;";

    let params = [
        req.body.gnum,
        req.body.cId
    ]

    let result = await db.execute(sql, params);

    console.log(result);

    let isModify = result && result.affectedRows >= 1;

    res.json({
        msg: isModify ? "修改成功" : "修改失败",
        code: 200
    })

})

module.exports = prodRouter;

